<?php

class GraduateController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        
    }

    public function profileAction()
    {
        $profileModule = new Application_Model_Graduate();
        $this->view->graduate = $profileModule->getGraduate($this->_request->getParam("email")); 
        if(empty($this->_request->getParam("email"))){
            $this->redirect('graduate/search');
        }
    }

    public function searchAction()
    {
        $profileModule = new Application_Model_Graduate();
        $baseUrl = Zend_Controller_Front::getBaseUrl();
         
        $this->view->graduates = $profileModule->getGraduates($baseUrl);
        $this->view->intakes = date("Y") - 2014 + 34;
    }

    public function graduatejsonAction()
    {
        $track = $this->_request->getParam("track");
        $intake =$this->_request->getParam("intake");
        $name = $this->_request->getParam("name"); 
        $offset = $this->_request->getParam("page");
        $baseUrl = Zend_Controller_Front::getBaseUrl();
        
        
        $profileModule = new Application_Model_Graduate();
        $this->view->graduates = $profileModule->getGraduates($baseUrl,$track,$intake,$name,$offset);
        $this->view->pagesNo = $profileModule->countPages($track,$intake,$name);
    }

    public function editAction()
    {
        $graduateModel = new Application_Model_Graduate();

        if($this->_request->isPost()){
            
            $form = new Application_Form_Editprofile();
            $postData = $this->_request->getParams();              

            if (!$form->isValid($postData)) {
                $this->view->form = $form;
                return $this->render();
            }
            
            $data['first_name'] = $postData['first_name'];
            $data['last_name'] = $postData['last_name'];
            $data['email'] = $postData['email'];
            $data['facebook'] = $postData['facebook'];
            $data['twitter'] = $postData['twitter'];
            $data['linkedin'] = $postData['linkedin'];
            $data['gender'] = $postData['gender'];
            $data['track_id'] = $postData['track_id'];
            $data['intake'] = $postData['intake'];
            $data['job'] = $postData['job'];
            $data['description'] = $postData['description'];
            $data['id'] = $postData['id']; 
            
            if(!empty($postData['password'])){
                $data['password'] = $postData['password'];
            }
            
            $upload = new Zend_File_Transfer(); 
           
            $path = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT').$this->view->baseUrl().'/images/icons/users/';// die($path);
            $info = $upload->getFileInfo();          
            $typeA = explode("/", $info['image']['type']);
            
            $upload->addFilter('Rename',
                   array ('target' => $path."user".$data['id'].".".$typeA[1],
                         'overwrite' => true));
            
            if ($upload->receive()) {
                   $data['image'] = "user".$data['id'].".".$typeA[1];              
            }
            $graduateModel->updateGraduate($data);
            
            $this->redirect("graduate/profile/username/".$this->_request->getParam("username"));
            
        }else{
            if(!empty($this->_request->getParam("username"))){
                $data = $graduateModel->getGraduate($this->_request->getParam("username")); 
                
                if(!empty($data)){
                    $this->view->form = new Application_Form_Editprofile();                
                    $this->view->form->populate($data);            
                    $this->render();
                }else{                    
                    $this->redirect("graduate/search");
                }
            }else{
                $this->redirect("graduate/search");
            }                        
        }
        
  }


}